home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18015 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.connect.net!usenet
  2. From: Roy Ivy III <roy@wizweb.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: "finally" in C++
  5. Date: Thu, 18 Apr 1996 10:34:37 -0500
  6. Organization: Wizards of the Web
  7. Message-ID: <3176610D.6FAD@wizweb.com>
  8. NNTP-Posting-Host: loish.wizweb.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.01Gold (Win95; I)
  13.  
  14. I'm trying to write a finally mechanism in C++. [Yes, I know that destructors do such things just fine, but I have reason 
  15. to get this working...]. I have a method which works but costs an exception throw _every_ time through the code whether or 
  16. not an exceptional condition has arisen.
  17.  
  18. Here's the method (macros shown - "-" marks end of particular macro):
  19.  
  20. foo()
  21. {
  22.     // WITHFINALLY
  23.     try { int _had_ex_ = 1;
  24.         // -
  25.         // TRY
  26.         try {
  27.         // -
  28.             fX(); // may throw any exception [including unknown execeptions]
  29.         // ENDTRY
  30.         }
  31.         // -
  32.         // CATCH (x)
  33.         catch (x &)
  34.         // -
  35.         { ... }
  36.         // ENDCATCH
  37.         // -
  38.     // FINALLY
  39.     _had_ex_ = 0;
  40.     throw "Do Finally";
  41.     }
  42.     catch (...)
  43.     {
  44.     // -
  45.     { ... <finalize code> ... }
  46.     // ENDFINALLY
  47.     if (_had_ex_) throw;
  48.     }
  49.     // -
  50.  
  51.     return;
  52. }
  53.  
  54.  
  55. Anyone have a better idea in which you only have to write the finalization code in one place, and doesn't cost an exception 
  56. to execute the finalization code in the normal (non-exceptional) case? 
  57. [You must assume the the finaliation code _cannot_ be done by a destructor!]
  58.  
  59. Anyone?
  60.  
  61. Post and email me any ideas.
  62.  
  63. Thanks.
  64.  
  65. - Roy Ivy III
  66.